// pages/mycard/edit/index.js
//import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog"
const uploadImage = require("../../../utils/uploadImg/uploadImg.js");
const rules = require('../../../utils/util.js')
const goods = require('../../../service/port')
const other = require('../../../service/other')
import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pickerIndex: 0,
    pickerList: ['否', '是'],
    cid1List: [],
    cid1TextList: [],
    cid2List: [],
    cid2TextList: [],
    goodsTextList: [],
    goodsList: [],
    goodPercentList:[],
    param: {
      cid1Index: '', //商品分类
      cid2Index: '', //商品分类
      goodsIndex: '', //商品
    },
    goods: '',
    name: '',
    phone: '',
    idCard1: '',
    idCard2: '',
    fileList1: [],
    fileList2: [],
    show:false
  },
  onLoad: function (options) {
    goods.getcategoryList1((res) => {
      let list = res.data
      let cid1TextList = []
      list.forEach(item => {
        cid1TextList.push(item.name)
      })
      this.setData({
        cid1List: list,
        cid1TextList: cid1TextList
      })
    })
  },
  bindPickerChange(e) {
    let index = e.detail.value
    this.setData({ pickerIndex: index })
  },
  bind1PickerChange(e) {
    let index = e.detail.value
    let cid1List = this.data.cid1List
    this.setData({
      'param.cid1Index': index,
      'param.cid2Index': '',
      'param.goodsIndex': '',
      // cid2List : [],
      // cid2TextList : [],
      goodsTextList: [],
      goodsList: [],
    })
    goods.getcategoryList2(cid1List[index].id, (res) => {
      let list = res.data || []
      let cid2TextList = []
      list.forEach(item => {
        cid2TextList.push(item.name)
      })
      this.setData({
        cid2List: list,
        cid2TextList: cid2TextList
      })
    })
  },
  bind2PickerChange(e) {
    let index = e.detail.value
    this.setData({
      'param.cid2Index': index,
      'param.goodsIndex': '',
      goodsTextList: [],
      goodsList: [],
    })
    let { cid1Index, cid2Index } = this.data.param
    let { cid1List, cid2List } = this.data
    if (cid1Index && cid2Index) {
      let param = {
        tbSpu: {
          cid1: cid1List[cid1Index].id,
          cid2: cid2List[cid2Index].id,
        }
      }
      other.getCommodityCategory(param, (res) => {
        let list = res.data
        let goodsTextList = []
        list.forEach(item => {
          goodsTextList.push(item.title)
        })
        this.setData({
          goodsList: list,
          goodsTextList: goodsTextList
        })
      })
    }

  },
  bindGoodsPickerChange(e) {
    let index = e.detail.value
    this.setData({
      'param.goodsIndex': index
    })
    other.getGoodpercent(this.data.goodsList[index].id,res=>{
      console.log(res)
      this.setData({
        goodPercentList:res.data
      })
    })
  },


  card1AfterRead(e) {
    var list = this.data.fileList1;
    list.push({
      url: e.detail.file.path,
      name: "1"
    });
    this.setData({
      fileList1: list,
    });
  },

  card2AfterRead(e) {
    var list = this.data.fileList2;
    list.push({
      url: e.detail.file.path,
      name: "2"
    });
    this.setData({
      fileList2: list,
    });
  },

  delete1photo() {
    this.setData({
      fileList1: []
    })
  },

  delete2photo() {
    this.setData({
      fileList2: []
    })
  },
  blurEvent(e) {
    console.log(111, e)
    let filed = e.currentTarget.dataset.filed
    this.setData({
      [filed]: e.detail.value
    })
  },
  submit() {

    let pickerIndex = this.data.pickerIndex
    if (this.data.param.cid1Index === '') {
      wx.showToast({
        title: '请选择一级分类',
        icon: 'none'
      })
      return
    }

    if (this.data.param.cid2Index === '') {
      wx.showToast({
        title: '请选择二级分类',
        icon: 'none'
      })
      return
    }

    if (this.data.param.goodsIndex === '') {
      wx.showToast({
        title: '请选择商品',
        icon: 'none'
      })
      return
    }


    if (!this.data.name) {
      wx.showToast({
        title: '请填写代理人姓名',
        icon: 'none'
      })
      return
    }

    if (!this.data.phone) {
      wx.showToast({
        title: '请填写代理人手机号',
        icon: 'none'
      })
      return
    }

    if (!this.data.idCard) {
      wx.showToast({
        title: '请填写代理人身份证号',
        icon: 'none'
      })
      return
    }
    this.setData({
      show:true
    })
    if (this.data.fileList1.length > 0) {
      let fileList1 = this.data.fileList1
      uploadImage(fileList1[0].url, "images/", (url1 => {
        this.setData({ idCard1: url1, })
        if (this.data.fileList2.length > 0) {
          let fileList2 = this.data.fileList2
          uploadImage(fileList2[0].url, "images/", (url2 => {
            this.setData({ idCard2: url2, })
            this.submitData()
          }))
        } else {
          wx.showToast({
            title: '请上传身份证国徽面',
            icon: 'none'
          })
        }
      }))
    } else {
      wx.showToast({
        title: '请上传身份证人像面',
        icon: 'none'
      })
    }
  },
  submitData() {

    let param = {
      data: {
        cutomerId: wx.getStorageSync("userInfo").id,//用户标识
        name: this.data.name,//姓名
        phone: this.data.phone,//电话
        idcardNum: this.data.idCard,//身份证号
        idcardPoitie: this.data.idCard1,//身份证正面图片地址
        idcardBack: this.data.idCard2,//身份证反面图片地址
        spuApplicationList: [{ spuId: this.data.goodsList[this.data.param.goodsIndex].id }]
      }
    }

    other.tAgentAdd(param, (res) => {
      this.setData({
        show:false
      })

      if (res.code == '00000') {
        wx.showToast({
          title: '申请成功!',
          duration: 3000,
          success: () => {
            wx.navigateBack({
              delta: 1,
            })
          }
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 3000,
        })
      }
    })






  }

})